package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.TMTxnBizOpMapper;
import cc.rengu.igas.momp.common.entity.TMTxnBizOp;
import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;

/**
 * 业务操作流水表数据库操作接口实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/8/19 11:39
 */

public class TMTxnBizOpMapperImpl implements TMTxnBizOpMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * 插入list数据数据库操作接口定义
     *
     * @param tmTxnBizOpList 业务操作流水表数据库实体类列表
     * @return int 返回码
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/8/19 11:41
     */
    @Override
    public int insertTmTxnBizOpList(List<TMTxnBizOp> tmTxnBizOpList) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertTmTxnBizOpList");

        for (TMTxnBizOp tmTxnBizOp : tmTxnBizOpList) {

            int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, tmTxnBizOp, TMTxnBizOp.class);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("插入法人机构号<{}>营销活动编号<{}>业务操作流水信息失败!<{}>", tmTxnBizOp.getInstId(), tmTxnBizOp.getMcNo(), iReturnCode);
                return iReturnCode;
            }
        }

        return Database.DBS_SUCCESS;
    }
}
